/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.displacements_view.command;

import java.awt.Component;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JFrame;

import br.ufmg.dees.insane.draw.GaussPointDraw;
import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationpoint.IntegrationPoint;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.NewMatrixViewTableDialog;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.TabbedDialog;
import br.ufmg.dees.insane.util.IMatrix;


/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/

public class SetUCommand implements Command, Cloneable{
	private LearnInterface parent;
	private Controller controller;
	private FemModel fmodel;
	
//*****************************************************************************
	
	/** Creates new SetElmAttributesCommand */
	public SetUCommand(JFrame parent) {
		this.parent = (LearnInterface)parent;
		controller = this.parent.getView().getController();
		fmodel = this.parent.getModel();
	}
	
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		ArrayList gPoints = controller.getSelection();
		
		if (gPoints.size() == 0) {
			parent.addOutputText("There is no gauss point in model.");
			return;
		}
	
		fmodel = parent.getView().getController().getModel();
		AnalysisModel anl = fmodel.getGlobalAnalysisModel();
		IMatrix N;
		int ngdl = anl.getNdf();
		NewMatrixViewTableDialog dlg;
		NewMatrixViewTableDialog dlg1;
		NewMatrixViewTableDialog dlg2;
		
		
        //Number Format
		DecimalFormat fmt = new DecimalFormat();
        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
        fsym.setDecimalSeparator('.');
        fmt.setDecimalFormatSymbols(fsym);
        fmt.applyPattern("0.000E00");
		
        // dialogo N
		ArrayList sel = this.parent.getView().getController().getSelection();
		ListIterator selLI = sel.listIterator();
		TabbedDialog dialog = new TabbedDialog();
		while(selLI.hasNext()){
		    GaussPointDraw ipd = (GaussPointDraw)selLI.next();
			IntegrationPoint ip = (IntegrationPoint)ipd.getRepresents();
			ParametricElement pelm = (ParametricElement)ipd.getOwner();
			N = pelm.mountN(ip);
			
			CmdButton[][] nMatrix = new CmdButton[N.getNumRow()][N.getNumCol()];
			for(int i =0; i< N.getNumRow(); i++)
				for(int j=0; j<N.getNumCol(); j++)
					nMatrix[i][j]=  new CmdButton(String.valueOf(fmt.format(N.getElement(i,j))));
			
			CmdButton[][] displacements = new CmdButton[N.getNumCol()][1];
			IMatrix D = new IMatrix(N.getNumCol(), 1);
			ArrayList no = pelm.getIncidence();
			ListIterator selNodes = no.listIterator();
			int cont =0;
			while(selNodes.hasNext()){
			    Node node = (Node)selNodes.next();
						for (int k=0; k<6; k++) {
							if (anl.getValidEquation(k)) {
								displacements[cont][0]=  new CmdButton(String.valueOf(fmt.format(node.getDisplacement(k))));
								D.setElement(cont, 0, node.getDisplacement(k));
								cont++;
							}
						}
			}
			
			String[] colNames = new String[N.getNumCol()];
			cont =0;
			for(int l=0; l<pelm.getIncidence().size(); l++){
			    String node = ((Node)pelm.getIncidence().get(l)).getLabel();
			    colNames[cont] = node;
			    colNames[cont+1] = node;
			    cont+=ngdl;
			}
			
			String[] lineNames = new String[1];
			for(int c=0; c<1; c++)
				lineNames[c] = " ";	
			
			//U
			N.mul(D);
			
			CmdButton[][] u = new CmdButton[N.getNumRow()][N.getNumCol()];
			for(int i =0; i< N.getNumRow(); i++)
				for(int j=0; j<N.getNumCol(); j++)
					u[i][j]=  new CmdButton(String.valueOf(fmt.format(N.getElement(i,j))));
			
		dlg = new NewMatrixViewTableDialog("Shape Matrix", ngdl, nMatrix[0].length, nMatrix );
		dlg1 = new NewMatrixViewTableDialog("Displacements",displacements.length, 1,displacements);
		dlg2 = new NewMatrixViewTableDialog("U", ngdl, 1,u);
			
		HypothesisPanel f = new HypothesisPanel(2,7,1,dlg,"N * ",dlg1,"d  ",dlg2,"U = ");
		dialog.addTab(pelm.getLabel()+"->"+ip.getLabel(), (Component)f);
		}		
		JFrame frame = new JFrame();
		
		dialog.showDialog(frame, " U = N * d  ");
    }
	
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	
	/** Clone method. */
	public Object clone() {
		return null;
	}
	
//*****************************************************************************


}

